package cn.edu.cug.cs.gtl.docsrv.txt.service.impl;

import cn.edu.cug.cs.gtl.docsrv.dict.exception.BaseException;
import cn.edu.cug.cs.gtl.docsrv.txt.dao.TxtDao;
import cn.edu.cug.cs.gtl.docsrv.txt.service.TxtService;
import cn.edu.cug.cs.gtl.mybatis.mapper.txt.TxtAll;
import cn.edu.cug.cs.gtl.mybatis.mapper.txt.TxtCatalog;
import cn.edu.cug.cs.gtl.mybatis.mapper.txt.TxtAll;
import cn.edu.cug.cs.gtl.mybatis.mapper.txt.TxtCatalog;
import cn.edu.cug.cs.gtl.mybatis.metadata.TxtUtils;
import cn.edu.cug.cs.gtl.mybatis.metadata.TxtUtils;
import cn.edu.cug.cs.gtl.protos.Document;
import cn.edu.cug.cs.gtl.protos.DocumentList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class TxtServiceImpl implements TxtService {

    @Autowired
    TxtDao txtDao;

    @Override
    public void addTxt(TxtAll txtAll) {
        txtDao.addTxt(txtAll);
    }

    @Override
    public void addTxts(List<TxtAll> txtAllList) {
        txtDao.addTxts(txtAllList);
    }

    @Override
    public boolean addFromRawDB() {
        return txtDao.addFromRawDB();
    }

    @Override
    public List<TxtAll> getTxtByName(String txtName) {
        return txtDao.getTxtByName(txtName);
    }

    @Override
    public List<TxtAll> getTxtByEnTitle(String txtEnTitle) {
        return txtDao.getTxtByEnTitle(txtEnTitle);
    }

    @Override
    public TxtAll getTxtByInternalId(BigDecimal txtInternalId) {
        return txtDao.getTxtByInternalId(txtInternalId);
    }

    @Override
    public boolean deleteTxtByInternalId(BigDecimal txtInternalId) {
        return txtDao.deleteTxtByInternalId(txtInternalId);
    }

    @Override
    public List<TxtCatalog> getTxtCatalogs() {
        return txtDao.getTxtCatalogs();
    }

    @Override
    public Document getDocumentByTxtInternalId(BigDecimal txtInternalId) {
        try {
            return txtDao.getDocumentByTxtInternalId(txtInternalId);
        } catch (Exception e) {
            throw new BaseException(e.getMessage());
        }
    }

    @Override
    public List<TxtAll> getTxtByCnTitle(String txtCnTitle) {
        return txtDao.getTxtByCnTitle(txtCnTitle);
    }

    @Override
    public DocumentList getDocumentsByCatalogInternalId(BigDecimal catalogInternalId) {
        try {
            try {
                List<TxtAll> txtAllList= txtDao.getTxtByCatalogInternalId(catalogInternalId);
                if(txtAllList!=null) {
                    DocumentList.Builder documentListBuilder = DocumentList.newBuilder();
                    for (TxtAll txtAll : txtAllList) {
                        Document doc = TxtUtils.convert(txtAll);
                        documentListBuilder.addElement(doc);
                    }
                    return documentListBuilder.build();
                }
                else
                    return null;
            } catch (Exception e) {
                throw new BaseException(e.getMessage());
            }
        } catch (Exception e) {
            throw new BaseException(e.getMessage());
        }
    }

    @Override
    public List<BigDecimal> getTxtInternalIdsByCatalogInternalId(BigDecimal catalogId) {
        return txtDao.getTxtInternalIdsByCatalogInternalId(catalogId);
    }
}
